#include "connection.h"

connection::connection()
{
    maxStorage=1000;
    Storage=0;
}

void connection::addConnection(string Protocol, string srcIP, string srcPort, string destIP, string destPort,long tm)
{
    int prot=0;
    int sp=0;
    int dp=0;

    if (Protocol.compare("UDP")==0)
    {
        prot=0;
    }
    else
    {
        prot=1;
    }

    sp=atoi(srcPort.c_str());
    dp=atoi(destPort.c_str());
    connection::addConnection(prot,srcIP,sp,destIP,dp,tm);


}

void connection::addConnection(int Protocol, string srcIP, int srcPort, string destIP, int destPort,long tm)
{
    if (Storage>=maxStorage)
    {
        data.erase(data.begin());
        Storage--;
    }

    connectionStructure temp;
    temp.protocol=Protocol;
    temp.srcIP=srcIP;
    temp.srcPort=srcPort;
    temp.destIP=destIP;
    temp.destPort=destPort;
    temp.tm=tm;

    data.push_back(temp);

    Storage++;
}

void connection::printConnections()
{
    for (int i=0;i<data.size();i++)
    {
        connectionStructure temp=data[i];
        cout << temp.protocol << " - " << temp.srcIP << " - " << temp.srcPort << " - " << temp.destIP << " - " << temp.destPort << " - " << temp.tm << endl;
    }
}

void connection::removeConnection(int idx)
{
    if ( (idx>=0) && (idx<Storage))
    {
        data.erase(data.begin()+idx);
        Storage--;
    }
}

connection::connectionStructure connection::getConnection(int idx)
{
    //cout << "idx=" << idx << endl;
    connectionStructure retVal;
    //cout << "Storage=" << Storage << endl;
    if ( (idx>=0) && (idx<Storage))
        retVal=data[idx];
    else
    {
        //cout << "Returning empty connection" << endl;
        retVal.protocol=-1;
        retVal.srcIP="";
        retVal.srcPort=-1;
        retVal.destIP="";
        retVal.destPort=-1;
        retVal.tm=0;
    }
    return retVal;
}
